package com.chenxingnet.service.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chenxingnet.dao.DepartmentMapper;
import com.chenxingnet.dao.PublicityMapper;
import com.chenxingnet.dao.UniteMapper;
import com.chenxingnet.dto.Department;
import com.chenxingnet.dto.Publicity;
import com.chenxingnet.dto.Unite;
import com.chenxingnet.dto.vo.meetVo;
import com.chenxingnet.service.IPublicityService;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
@Service("publicityService")
public class PublicityServiceImpl   implements  IPublicityService
{

	
	@Resource
	PublicityMapper publicity;
	
	@Resource
	UniteMapper  uniteMapper;
	
	@Resource
	DepartmentMapper   departmentMapper;  
	
	
	public int insertSelective(Publicity record) {
		
		int res= publicity.insertSelective(record);
		
		return   res;
		
		
	}
	
	/*
	 * 开启事务进行插入会议提醒
	 */
	@Transactional(rollbackFor=Throwable.class)	
	public int insertSelectiveAndInsert(Publicity record,String   depid) {
		int  res=0;
		int publicityID= publicity.insertSelectiveMy(record);
		
		Unite  un=new  Unite();
		un.setDeptid(Integer.parseInt(depid));
		un.setUreportstart("1");
		un.setPid(record.getPid());
		res=	uniteMapper.insertSelective(un);
		
		return   res;
	}
	

	public List<Publicity> getAllMeetInfo() {
		return publicity.getAllMeetInfo();
	}

	public meetVo selectByid(Integer id) {
		meetVo mv = new meetVo();
		
		Publicity publi = new Publicity();
		publi.setPid(id);
		publi.setPreportstart("0");
		int i = publicity.updateByPrimaryKeySelective(publi);
		if(i != 0){
			Publicity pub = publicity.selectByPrimaryKey(id);
			mv.setPid(pub.getPid());
			mv.setPcontent(pub.getPcontent());
			mv.setPstart(pub.getPreportstart());
			Unite unite = uniteMapper.selectByPid(pub.getPid());
			Department dept = departmentMapper.selectByPrimaryKey(unite.getDeptid());
			mv.setDeptName(dept.getDeptname());
		}
		
		return mv;
	}

	public List<meetVo> selectAll() {
		List<Publicity> list = publicity.getAllMeetInfo();
		List<meetVo> mvList = new ArrayList<meetVo>();
		for (int i = 0; i < list.size(); i++) {
			meetVo mv = new meetVo();
			mv.setPid(list.get(i).getPid());
			mv.setPcontent(list.get(i).getPcontent());
			mv.setPstart(list.get(i).getPreportstart());
			Unite unite = uniteMapper.selectByPid(list.get(i).getPid());
			Department dept = departmentMapper.selectByPrimaryKey(unite.getDeptid());
			mv.setDeptName(dept.getDeptname());
			mvList.add(mv);
			
		}
		
		return mvList;
	}

	public List<meetVo> selectByDept(Integer deptid) {
		
		List<Publicity> list = publicity.getAllByDept(deptid);
		List<meetVo> mvList = new ArrayList<meetVo>();
		for (int i = 0; i < list.size(); i++) {
			meetVo mv = new meetVo();
			mv.setPid(list.get(i).getPid());
			mv.setPcontent(list.get(i).getPcontent());
			mv.setPstart(list.get(i).getPreportstart());
			/*Unite unite = uniteMapper.selectByPid(list.get(i).getPid());*/
			Department dept = departmentMapper.selectByPrimaryKey(deptid);
			mv.setDeptName(dept.getDeptname());
			mvList.add(mv);
			
		}
		
		return mvList;
	}

	/**
	 * 需要根据id和deptid获取到值
	 */
	public meetVo selectByidAndDept(Integer id, Integer deptid) {
		meetVo mv = new meetVo();
		
		Publicity publi = new Publicity();
		publi.setPid(id);
		publi.setPreportstart("0");
		int i = publicity.updateByPrimaryKeySelective(publi);
		if(i != 0){
			Publicity pub = publicity.selectByIdAndDept(id, deptid);
			mv.setPid(pub.getPid());
			mv.setPcontent(pub.getPcontent());
			mv.setPstart(pub.getPreportstart());
			Unite unite = uniteMapper.selectByPid(pub.getPid());
			Department dept = departmentMapper.selectByPrimaryKey(unite.getDeptid());
			mv.setDeptName(dept.getDeptname());
		}
		
		return mv;
	}

	public List<Publicity> getAllMeetByDept(Integer deptid) {
		
		return publicity.getAllByDept(deptid);
	}
	
	
	

}
